*******************************************************************************
* Specify city; load data
*******************************************************************************

local city = "fort_worth"

use dataset_`city'_panel, clear

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distance {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distance = -distance + 1

*******************************************************************************
* Time interactions
*******************************************************************************

gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable pctnhblackXy2 "Share Black X 2014"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2014"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctyes pctnhblackXy2 pctnhblackX2020 ///
	distanceXy2 distanceX2020 year_2 year_3 ///
	[aw=totalvotes], robust cl(precinct) absorb(precinct)
unique precinct if e(sample)
estadd local nclust = r(unique)
estadd local yfe = "X"
estadd local pfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local myse = "Precinct-"
estadd local myse2 = "clustered"

foreach l in 1 2 5 {
	eststo: xi: acreg pctyes pctnhblackXy2 pctnhblackX2020 ///
		distanceXy2 distanceX2020 year_2 year_3 ltotalvotes, ///
		pfe1(id) spatial latitude(y_c) longitude(x_c) ///
		distcutoff(`l') hac id(precinct) time(year) lagcutoff(12)
	estadd local slag = `l'
	unique precinct if e(sample)
	estadd local nclust = r(unique)
	estadd local yfe = "X"
	estadd local pfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local myse = "HAC"
	estadd local myse2 ""
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A15.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		drop(_cons year_* ltotalvotes)
		stats(yfe pfe wtvote clvote myse myse2 slag N nclust, 
			label(
				"Year Fixed Effects"
				"Precinct Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Standard Errors"
				" "
				"Distance Cutoff (Miles)"
				"Number of Observations"
				"Number of Unique Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		gap
		alignment(l)
		;
			
#delimit cr	

*******************************************************************************
* End
*******************************************************************************
